package it.sciuto.greetings.adapters;

import it.sciuto.greetings.R;
import it.sciuto.greetings.activities.ZoomActivity;

import java.io.File;
import java.util.LinkedList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class FolderAdapter extends BaseAdapter {
	private Activity activity;
	private LinkedList<String> images;

	public FolderAdapter(Activity activity, LinkedList<String> images) {
		this.activity = activity;
		this.images = images;
	}

	public int getCount() {
		return images.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.folder_row, null);
		}

		final String filename = "/mnt/sdcard/greetings/" + images.get(position);

		// Image View Preview
		ImageView iv = (ImageView) convertView.findViewById(R.id.list_preview);
		iv.setImageURI(Uri.parse(filename));
		iv.setScaleType(ImageView.ScaleType.FIT_XY);
		iv.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Intent mIntent = new Intent(activity, ZoomActivity.class);
				Bundle mBundle = new Bundle();
				mBundle.putString("filename", filename);
				mIntent.putExtras(mBundle);
				activity.startActivity(mIntent);
			}
		});

		// Title
		TextView dateTextView = (TextView) convertView.findViewById(R.id.list_date);
		dateTextView.setText(filename.split("/")[filename.split("/").length - 1]);
		dateTextView.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Intent mIntent = new Intent(activity, ZoomActivity.class);
				Bundle mBundle = new Bundle();
				mBundle.putString("filename", filename);
				mIntent.putExtras(mBundle);
				activity.startActivity(mIntent);
			}
		});

		// Delete Button
		ImageButton deleteButton = (ImageButton) convertView.findViewById(R.id.list_delete_button);
		deleteButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(activity);
				builder.setMessage(R.string.delete_question).setCancelable(false).setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						images.remove(position);
						new File(filename).delete();
						notifyDataSetChanged();
					}
				}).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
				AlertDialog alert = builder.create();
				alert.show();
			}
		});

		// Share Button
		ImageButton shareButton = (ImageButton) convertView.findViewById(R.id.list_share_button);
		shareButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Intent share = new Intent(Intent.ACTION_SEND);
				share.setType("image/jpeg");

				share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filename));
				activity.startActivity(Intent.createChooser(share, activity.getString(R.string.share_title)));
			}
		});
		return convertView;
	}

}